package com.sara.service;

import com.sara.dao.MovieDao;
import com.sara.dao.MovieTypeDao;
import com.sara.dao.ReplyDao;
import com.sara.dao.TypeDao;
import com.sara.entity.Movie;
import com.sara.entity.Type;
import com.sara.util.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MovieService  {
    private TypeDao typeDao = new TypeDao();
    private ReplyDao replyDao = new ReplyDao();
    private MovieDao movieDao = new MovieDao();
    private MovieTypeDao movieTypeDao = new MovieTypeDao();

    public Page<Movie> findMovieByPage(String p, String keys) {
        p = StringUtils.isEmpty(p) ? "1" : p;
        p = StringUtils.isNumeric(p) ? p : "1";

        int count = movieDao.count(keys);
        Page<Movie> page = new Page<>(Integer.valueOf(p), count);
        Map<String, String> params = new HashMap<>();
        params.put("start", page.getStart().toString());
        params.put("pageSize", page.getPageSize().toString());
        params.put("keys", keys);

        List<Movie> movieList = movieDao.queryMovieByParams(params);

        for(Movie movie : movieList){
            List<Type> typeList = typeDao.queryByMovieId(movie.getId());
            movie.setTypeList(typeList);
        }
        page.settList(movieList);
        return  page;

    }
}
